#######################################
# Replication file for Salta Province #
#######################################


rm(list=ls(all=TRUE))
#setwd("")
library(eiPack)
library(coda)
library(foreign)

data=read.csv(file.path(getwd(),"data","SALTA_data.csv"))


# Define row and column marginals of the transition matrix. 
GFPV=data$gral_501
GFPS=data$gral_535
GSST=data$gral_203
GO=data$gral_14+data$gral_155
GN=data$votantes-GFPV-GFPS-GSST-GO


PFPVA=data$paso_3086
PFPVB=data$paso_3087
PFPS=data$paso_535
PSST=data$paso_203
PO=data$paso_14+data$paso_155+data$paso_38+data$paso_536
PN=data$votantes-PFPVA-PFPVB-PFPS-PSST-PO


data1=data.frame(PFPVA,PFPVB,PFPS,PSST,PO,PN,GFPV,GFPS,GSST,GO,GN)


# Tunning the EI algorithm
tune.nocov=tuneMD(cbind(GFPV,GFPS,GSST,GO,GN)~cbind(PFPVA,PFPVB,PFPS,PSST,PO,PN),
                  data=data1,ntunes=1,totaldraws=200000)


# MCMC
out.nocov=ei.MD.bayes(cbind(GFPV,GFPS,GSST,GO,GN)~cbind(PFPVA,PFPVB,PFPS,PSST,PO,PN),
                      covariate=NULL,data=data1,tune.list=tune.nocov,verbose=0,ret.beta="r",thin=80,burnin=100000,ret.mcmc=TRUE) 

# Obtain transition matrix for each mesa: 
source(file.path(getwd(),"Codes","getbetas.R"))
beta11=getbetas(out.nocov,"PFPVA","GFPV")
beta12=getbetas(out.nocov,"PFPVA","GFPS")
beta13=getbetas(out.nocov,"PFPVA","GSST")
beta14=getbetas(out.nocov,"PFPVA","GO")
beta15=getbetas(out.nocov,"PFPVA","GN")
beta21=getbetas(out.nocov,"PFPVB","GFPV")
beta22=getbetas(out.nocov,"PFPVB","GFPS")
beta23=getbetas(out.nocov,"PFPVB","GSST")
beta24=getbetas(out.nocov,"PFPVB","GO")
beta25=getbetas(out.nocov,"PFPVB","GN")
beta31=getbetas(out.nocov,"PFPS","GFPV")
beta32=getbetas(out.nocov,"PFPS","GFPS")
beta33=getbetas(out.nocov,"PFPS","GSST")
beta34=getbetas(out.nocov,"PFPS","GO")
beta35=getbetas(out.nocov,"PFPS","GN")
beta41=getbetas(out.nocov,"PSST","GFPV")
beta42=getbetas(out.nocov,"PSST","GFPS")
beta43=getbetas(out.nocov,"PSST","GSST")
beta44=getbetas(out.nocov,"PSST","GO")
beta45=getbetas(out.nocov,"PSST","GN")
beta51=getbetas(out.nocov,"PO","GFPV")
beta52=getbetas(out.nocov,"PO","GFPS")
beta53=getbetas(out.nocov,"PO","GSST")
beta54=getbetas(out.nocov,"PO","GO")
beta55=getbetas(out.nocov,"PO","GN")
beta61=getbetas(out.nocov,"PN","GFPV")
beta62=getbetas(out.nocov,"PN","GFPS")
beta63=getbetas(out.nocov,"PN","GSST")
beta64=getbetas(out.nocov,"PN","GO")
beta65=getbetas(out.nocov,"PN","GN")
betaout=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)

write.table(betaout,file=file.path(getwd(),"Output","SALTA_Mesa.txt"),col.names=NA)


# Obtain aggregate transition matrix. 
source(file.path(getwd(),"Codes","getbetasagr.R"))
beta11=getbetasagr(out.nocov,"PFPVA","GFPV")
beta12=getbetasagr(out.nocov,"PFPVA","GFPS")
beta13=getbetasagr(out.nocov,"PFPVA","GSST")
beta14=getbetasagr(out.nocov,"PFPVA","GO")
beta15=getbetasagr(out.nocov,"PFPVA","GN")
beta21=getbetasagr(out.nocov,"PFPVB","GFPV")
beta22=getbetasagr(out.nocov,"PFPVB","GFPS")
beta23=getbetasagr(out.nocov,"PFPVB","GSST")
beta24=getbetasagr(out.nocov,"PFPVB","GO")
beta25=getbetasagr(out.nocov,"PFPVB","GN")
beta31=getbetasagr(out.nocov,"PFPS","GFPV")
beta32=getbetasagr(out.nocov,"PFPS","GFPS")
beta33=getbetasagr(out.nocov,"PFPS","GSST")
beta34=getbetasagr(out.nocov,"PFPS","GO")
beta35=getbetasagr(out.nocov,"PFPS","GN")
beta41=getbetasagr(out.nocov,"PSST","GFPV")
beta42=getbetasagr(out.nocov,"PSST","GFPS")
beta43=getbetasagr(out.nocov,"PSST","GSST")
beta44=getbetasagr(out.nocov,"PSST","GO")
beta45=getbetasagr(out.nocov,"PSST","GN")
beta51=getbetasagr(out.nocov,"PO","GFPV")
beta52=getbetasagr(out.nocov,"PO","GFPS")
beta53=getbetasagr(out.nocov,"PO","GSST")
beta54=getbetasagr(out.nocov,"PO","GO")
beta55=getbetasagr(out.nocov,"PO","GN")
beta61=getbetasagr(out.nocov,"PN","GFPV")
beta62=getbetasagr(out.nocov,"PN","GFPS")
beta63=getbetasagr(out.nocov,"PN","GSST")
beta64=getbetasagr(out.nocov,"PN","GO")
beta65=getbetasagr(out.nocov,"PN","GN")
betaoutagr=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)

betaoutagr=as.vector(betaoutagr[1,1:120])
betaoutagr=matrix(betaoutagr,4)
betaoutagr_coefs=t(matrix(betaoutagr[1,],5))
betaoutagr_sd=t(matrix(betaoutagr[2,],5))
write.table(betaoutagr_coefs,file=file.path(getwd(),"Output","SALTAAggregate_Coefs.txt"),col.names=NA)
write.table(betaoutagr_sd,file=file.path(getwd(),"Output","SALTAAggregate_SD.txt"),col.names=NA)


# save(out.nocov, file = "SALTAMCMC.Rdata") # Save MCMC runs


# Geweke Convergence Statistics
geweke=geweke.diag(out.nocov$draws$Beta)
gewekestats=geweke[[1]]
gewekedf=data.frame(gewekestats)
#write.table(gewekedf,file="SALTAgewekelist.txt",col.names=NA)

jpeg(file.path(getwd(),"Output","SALTAHistogramGeweke.jpeg"))
hist(gewekestats)
dev.off()

print("Max Geweke")
print(max(gewekestats))
print("Min Geweke")
print(min(gewekestats))
print("Percentage Above 2.35")
print(length(gewekestats[abs(gewekestats)>2.35])/length(gewekestats))
print("Percentage Above 1.96")
print(length(gewekestats[abs(gewekestats)>1.96])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1.5])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1])/length(gewekestats))
print("Percentage Above 0.5")
print(length(gewekestats[abs(gewekestats)>0.5])/length(gewekestats))
